package com.google.memoryofaplace.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.repackaged.org.json.JSONObject;
import com.google.memoryofaplace.BLDenounces;
import com.google.memoryofaplace.BLPlaceComments;
import com.google.memoryofaplace.BLPlaceMedia;
import com.google.memoryofaplace.BLPlaces;
import com.google.memoryofaplace.Codes;
import com.google.memoryofaplace.General;

public class RPCHandler extends HttpServlet {
	private static final long serialVersionUID = 8410724390309880878L;

	public void doGet(HttpServletRequest req, HttpServletResponse res)
			throws IOException {
		// redirect to post
		doPost(req, res);
	}

	public void doPost(HttpServletRequest req, HttpServletResponse res)
			throws IOException {
		res.setCharacterEncoding("UTF-8");
		res.setContentType("application/json;charset=UTF-8");
		String action = req.getParameter("action");
		String strValidation = General.validateParams(req, General.RPC_ACTIONS);
		JSONObject jReturn;

		try {
			if (!strValidation.equals("")) {
				res.getWriter().print(
						General.returnError(General
								.getLocaleMessage("invalidParams")));
			} else {
				// PLACES
				if (action.equals("addPlace")) {
					if ((Codes.PlaceType.find(Integer.parseInt(req
							.getParameter("arg2"))) == null)
							|| (Codes.Status.find(Integer.parseInt(req
									.getParameter("arg3"))) == null)
							|| (Codes.PlaceCategoryType.find(Integer
									.parseInt(req.getParameter("arg4"))) == null)) {
						jReturn = General.returnError(General
								.getLocaleMessage("invalidParams"));
					} else {
						jReturn = BLPlaces.createPlace(
								req.getParameter("arg0"),
								req.getParameter("arg1"),
								req.getParameter("arg2"),
								req.getParameter("arg3"),
								req.getParameter("arg4"),
								req.getParameter("arg5"),
								req.getParameter("arg6"),
								req.getParameter("arg7"),
								req.getParameter("arg8"),
								req.getParameter("arg9"),
								req.getParameter("arg10"),req.getParameter("arg11"));
					}
					res.getWriter().print(jReturn.toString());
				}
				if (action.equals("getPlaces")) {
					if ((req.getParameter("arg0") != null)
							&& (req.getParameter("arg1") != null)
							&& (req.getParameter("arg2") != null)
							&& (req.getParameter("arg3") != null)) {

						if (req.getParameter("p") != null) {
							jReturn = BLPlaces
									.getPlaces(Double.parseDouble(req
											.getParameter("arg0")), Double
											.parseDouble(req
													.getParameter("arg1")),
											Double.parseDouble(req
													.getParameter("arg2")),
											Double.parseDouble(req
													.getParameter("arg3")),
											Integer.parseInt(req
													.getParameter("p")));
						} else {

							jReturn = BLPlaces
									.getPlaces(Double.parseDouble(req
											.getParameter("arg0")), Double
											.parseDouble(req
													.getParameter("arg1")),
											Double.parseDouble(req
													.getParameter("arg2")),
											Double.parseDouble(req
													.getParameter("arg3")));
						}
					} else {
						jReturn = BLPlaces.getPlaces();

					}
					res.getWriter().print(jReturn.toString());
				}

				if (action.equals("getPlace")) {
					jReturn = BLPlaces.getPlace(Long.parseLong(req
							.getParameter("arg0")));
					res.getWriter().print(jReturn.toString());
				}

				if (action.equals("getLastPlaces")) {
					jReturn = BLPlaces.getLastPlaces();
					res.getWriter().print(jReturn.toString());
				}

				if (action.equals("getLastPlacesMedia")) {
					jReturn = BLPlaces.getLastPlacesMedia();
					res.getWriter().print(jReturn.toString());
				}

				if (action.equals("getPlaceMedia")) {
					jReturn = BLPlaces.getPlaceMedia(
							Long.parseLong(req.getParameter("arg0")),
							Long.parseLong(req.getParameter("arg1")));
					res.getWriter().print(jReturn.toString());
				}

				if (action.equals("getPlaceMediaList")) {
					int page = (req.getParameter("p") == null) ? 1 : Integer
							.parseInt(req.getParameter("p"));
					int filter = (req.getParameter("f") == null) ? 0 : Integer
							.parseInt(req.getParameter("f"));
					if (filter != 0) {
						if (Codes.MediaType.find(filter) == null) {
							jReturn = General.returnError(General
									.getLocaleMessage("invalidParams"));
						} else {
							jReturn = BLPlaces.getPlaceMedia(
									Long.parseLong(req.getParameter("arg0")),
									page, filter);
						}
					} else {
						jReturn = BLPlaces.getPlaceMedia(
								Long.parseLong(req.getParameter("arg0")), page,
								filter);
					}
					res.getWriter().print(jReturn.toString());
				}

				if (action.equals("getPlaceComments")) {
					int page = (req.getParameter("p") == null) ? 1 : Integer
							.parseInt(req.getParameter("p"));
					jReturn = BLPlaces.getPlaceComments(
							Long.parseLong(req.getParameter("arg0")), page);
					res.getWriter().print(jReturn.toString());
				}

				// PLACE VIDEOS
				if (action.equals("addPlaceMedia")) {
					if ((Codes.MediaType.find(Integer.parseInt(req
							.getParameter("arg5"))) == null)
							|| (Codes.MediaContentType.find(Integer
									.parseInt(req.getParameter("arg6"))) == null)) {
						jReturn = General.returnError(General
								.getLocaleMessage("invalidParams"));
					} else {
						
						
						SimpleDateFormat formatter = new SimpleDateFormat(
								"dd/MM/yyyy");
						Date taken = null;
						if (req.getParameter("arg7")!=null)
							taken = formatter.parse(req.getParameter("arg7"));

						jReturn = BLPlaceMedia.addPlaceMedia(
								Long.parseLong(req.getParameter("arg0")),
								req.getParameter("arg1"),
								req.getParameter("arg2"),
								req.getParameter("arg3"),
								req.getParameter("arg4"),
								Integer.parseInt(req.getParameter("arg5")),
								Integer.parseInt(req.getParameter("arg6")),
								taken, req.getParameter("arg8"));
					}
					res.getWriter().print(jReturn.toString());
				}
				// PLACE COMMENTS
				if (action.equals("addPlaceComment")) {
					if ((Codes.CommentType.find(Integer.parseInt(req
							.getParameter("arg3"))) == null)) {
						jReturn = General.returnError(General
								.getLocaleMessage("invalidParams"));
					} else {
						jReturn = BLPlaceComments.addPlaceComment(
								Long.parseLong(req.getParameter("arg0")),
								req.getParameter("arg1"),
								req.getParameter("arg2"),
								Integer.parseInt(req.getParameter("arg3")));
					}
					res.getWriter().print(jReturn.toString());
				}

				// DENOUNCES
				if (action.equals("denounce")) {
					if ((Codes.DenounceObjectType.find(Integer.parseInt(req
							.getParameter("arg2"))) == null)
							|| ((Codes.DenounceType.find(Integer.parseInt(req
									.getParameter("arg3"))) == null))) {
						jReturn = General.returnError(General
								.getLocaleMessage("invalidParams"));

					} else {
						jReturn = BLDenounces.denounce(
								Long.parseLong(req.getParameter("arg0")),
								Long.parseLong(req.getParameter("arg1")),
								Integer.parseInt(req.getParameter("arg2")),
								Integer.parseInt(req.getParameter("arg3")));
					}
					res.getWriter().print(jReturn.toString());
				}
				if (action.equals("getDenounces")) {
					jReturn = BLDenounces.getDenounces();
					res.getWriter().print(jReturn.toString());
				}

			}
		} catch (IllegalArgumentException e) {
			res.getWriter().print(
					General.returnError(General
							.getLocaleMessage("invalidParams")));
		} catch (Exception e) {
			res.getWriter().print(
					General.returnError("error:" + e.getMessage()));
		}

	}
}
